﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTOFrameworks;
using FooFun.DataElements.Dtos;
using FAQFrameworks;
using System.Data.Common;

namespace PhysicalDataProvider
{
    public class UserService
    {
        FAQUser GetUserById(Guid id)
        {
            DataTable table = DataService.FillTable(
                @"SELECT * FROM t_Users,t_Accounts WHERE 
t_Accounts.AccountId = t_Users.UserId AND UserId ='"
                + DataProviderUtil.GetGuidBase64(id) + "'");

            DataRow row = table.Rows[0];

            FAQUser user = new FAQUser();
            user.LoginName = (string)row["LoginName"];

            Account account = new Account();
            account.AccountName = (string)row["AccountName"];
            account.AccountId
                = DataProviderUtil.GetGuid(row["AccountId"]);

            user.Account = account;

            return user;
        }

        public IDataElement GetElement(object obj)
        {
            FAQUser user = null;
            try
            {
                user = GetUserById((Guid)obj);
            }
            catch { }
            if (user != null)
            {
                return new UserDataElement(user);
            }
            else
            {
                return new EmptyDataElement();
            }
        }

        public IDataElement GetElement(IDataElement obj)
        {
            FAQUser user = null;
            try
            {
                user = GetUserById(
                    DataProviderUtil.GetGuid(obj.Value));
            }
            catch { }
            if (user != null)
            {
                return new UserDataElement(user);
            }
            else
            {
                return new EmptyDataElement();
            }
        }

        public void ChangePassword(Guid userId, string str)
        {
            Guid guid = Guid.NewGuid();
            string saltStr = guid.ToString();

            Guid encodedPwd
                = AuthenticationUtil.MakeMD5Hash(guid, str);

            SqlHelper.ExecuteNonQuery(
                string.Format(@"UPDATE t_Users 
SET [Password]='{0}',EncryptCode='{1}'
WHERE UserId='{2}'", encodedPwd, saltStr,
DataProviderUtil.GetGuidBase64(userId)),
new DbParameter[] { });
        }

        public class UserDataElement : CustomDataElement, IEnumItem
        {
            FAQUser user;
            public UserDataElement(FAQUser user)
                : base(new ObjectDataElement(user))
            {
                this.user = user;
            }

            #region IEnumItem 成员

            public string Name
            {
                get
                {
                    return user.Account.AccountName;
                }
            }

            public Guid Id
            {
                get
                {
                    return user.Account.AccountId;
                }
            }

            #endregion
        }
    }
}
